package com.tensquare.qa.service;


import com.tensquare.qa.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.tensquare.qa.dao.CommentDao;

import java.util.Date;
import java.util.List;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class CommentService {

	@Autowired
	private CommentDao commentDao;
	
	@Autowired
	private IdWorker idWorker;
	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 查询全部记录
	 *
	 * @return
	 */
	public List<Comment> findAll() {
		return commentDao.findAll();
	}

	/**
	 * 根据主键查询实体
	 *
	 * @param id
	 * @return
	 */
	public Comment findById(String id) {
		Comment comment = commentDao.findById(id).get();
		return comment;

	}

	/**
	 * 增加
	 *
	 * @param comment
	 */
	public void add(Comment comment) {
		comment.set_id(idWorker.nextId() + ""); //主键值
		comment.setCreatetime(new Date());//发布日期
		commentDao.save(comment);

	}

	/**
	 * 修改
	 *
	 * @param comment
	 */

	public void update(Comment comment) {
		commentDao.save(comment);
	}

	/**
	 * 删除
	 *
	 * @param id
	 */
	public void deleteById(String id) {
		commentDao.deleteById(id);
	}


	/**
	 * 根据问题id查询评论
	 */
//	public Page<Comment> findByParentid(String problemid) {
//		Pageable pageable = PageRequest.of(0,2147483647);
//		//return commentDao.findByProblemid(problemid, pageable);
//		return commentDao.findByProblemid(problemid, pageable);
//	}

	/**
	 * 根据问题id和回答id查询评论
	 */
	public Page<Comment> findByProblemidAndReplyid(String problemid,String replayid) {
		Pageable pageable = PageRequest.of(0,2147483647);
		return commentDao.findByProblemidAndReplyid(problemid,replayid,pageable);
	}
}
